﻿using HelixToolkit.Wpf;
using System;
using System.Collections.Generic;
using System.Diagnostics.Metrics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Media;
using System.Windows.Media.Media3D;

namespace QuickStart_Project_01.Model3Ds
{
    public class HouseModel3D : Visual3Dobjet
    {
        GeometryModel3D roof = new GeometryModel3D();
        GeometryModel3D wall = new GeometryModel3D();

        public HouseModel3D()
        {
            roof.Material = MaterialHelper.CreateMaterial(Colors.Brown, 0.5);
            wall.Material = MaterialHelper.CreateMaterial(Colors.LightGray, 0.5);

            this.InitModel();

            Model3DGroup group = new Model3DGroup();
            group.Children.Add(roof);
            group.Children.Add(wall);
            this.Visual3DModel = group;
        }

        public override void Selected(bool selected)
        {
            double opacity = 0.5;
            if (selected)
            {
                opacity = 1;
            }
            roof.Material = MaterialHelper.CreateMaterial(Colors.Brown, opacity);
            wall.Material = MaterialHelper.CreateMaterial(Colors.LightGray, opacity);
        }

        private void InitModel()
        {
            double width = 10, height = 2.5, length = 20;
            double roof_angle = 15;

            // 房顶
            MeshBuilder roof_builder = new MeshBuilder(false, false);
            var theta = Math.PI * roof_angle / 180;
            var y = height + Math.Tan(theta) * width / 2;

            var p0 = new Point(0, y);
            var p1 = new Point(width / 2 + 0.2 * Math.Cos(theta), height - 0.2 * Math.Sin(theta));
            var p2 = new Point(p1.X + 0.2 * Math.Sin(theta), p1.Y + 0.2 * Math.Cos(theta));
            var p3 = new Point(0, y + 0.2 / Math.Cos(theta));
            var p4 = new Point(-p2.X, p2.Y);
            var p5 = new Point(-p1.X, p1.Y);

            var roof_section = new[] { p0, p1, p1, p2, p2, p3, p3, p4, p4, p5, p5, p0 };
            roof_builder.AddExtrudedSegments(
                roof_section,
                new Vector3D(0, -1, 0),
                new Point3D(-length / 2, 0, 0),
                new Point3D(length / 2, 0, 0));

            var cap = new[] { p0, p1, p2, p3, p4, p5 };
            roof_builder.AddPolygon(cap, new Vector3D(0, -1, 0), new Vector3D(0, 0, 1), new Point3D(-length / 2, 0, 0));
            roof_builder.AddPolygon(cap, new Vector3D(0, 1, 0), new Vector3D(0, 0, 1), new Point3D(length / 2, 0, 0));

            this.roof.Geometry = roof_builder.ToMesh(true);

            // 墙体
            MeshBuilder wall_builder = new MeshBuilder(false, false);
            wall_builder.AddBox(new Point3D(0, 0, height / 2),
                length, width, height);

            wall_builder.AddPolygon(new[] { p0, new Point(width / 2, height), new Point(-width / 2, height) },
            new Vector3D(0, -1, 0), new Vector3D(0, 0, 1), new Point3D(-length / 2, 0, 0));

            wall_builder.AddPolygon(new[] { p0, new Point(width / 2, height), new Point(-width / 2, height) },
            new Vector3D(0, 1, 0), new Vector3D(0, 0, 1), new Point3D(length / 2, 0, 0));

            wall.Geometry = wall_builder.ToMesh();
        }
    }
}
